package com.cfpl.dealer_service.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfpl.admin_vegetable_service.mapper.WarehouseMapper;
import com.cfpl.dealer_service.VO.DistributeVO;
import com.cfpl.dealer_service.service.DistributeInformationService;
import com.cfpl.project_common.enums.CodeEnum;
import com.cfpl.project_common.exception.BusException;
import com.cfpl.project_common.pojo.DistributeInformation;
import com.cfpl.dealer_service.mapper.DistributeInformationMapper;
import com.cfpl.project_common.pojo.Warehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 14720
* @description 针对表【distribute_information(分发经销商信息表)】的数据库操作Service实现
* @createDate 2023-11-10 14:52:51
*/
@Service
public class DistributeInformationServiceImpl extends ServiceImpl<DistributeInformationMapper, DistributeInformation>
    implements DistributeInformationService {

    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private DistributeInformationMapper distributeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDistributeRecordInfo(DistributeInformation distributeInformation) {
        Warehouse warehouse = warehouseMapper.selectById(distributeInformation.getWarehouseId());
        //校验库存信息
        if (warehouse.getProductNumber() - warehouse.getProductSellNumber() - distributeInformation.getDeliveryNum() < 0){
            throw new BusException(CodeEnum.INSUFFICIENT_STOCK_ERROR);
        }
        //扣减库存
        warehouseMapper
                .updateProductSellNum(distributeInformation.getDeliveryNum(), distributeInformation.getWarehouseId());
        //库存充足 -> 添加分发商品信息
        distributeMapper.insert(distributeInformation);

    }

    /**
     * 查询经销商，分发商 详细信息
     * @param page
     * @param condition
     * @return
     */
    @Override
    public IPage<DistributeVO> pageQueryDistribute(IPage<DistributeVO> page, String condition) {

        return distributeMapper.pageQueryDistribute(page,condition) ;
    }


}




